package com.huaboot.system.controller;

import com.huaboot.commont.constant.Constant;
import com.huaboot.commont.core.BaseResponse;
import com.huaboot.commont.core.ResultUtils;
import com.huaboot.framework.log.annotations.OperateLog;
import com.huaboot.framework.log.enums.OperateTypeEnum;
import com.huaboot.framework.security.user.LoginUser;
import com.huaboot.framework.security.utils.SecurityUtil;
import com.huaboot.system.convert.SysMenuConvert;
import com.huaboot.system.entity.SysMenuEntity;
import com.huaboot.system.enums.MenuTypeEnum;
import com.huaboot.system.service.SysMenuService;
import com.huaboot.system.vo.SysMenuVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 菜单管理
 *

 */
@RestController
@RequestMapping("sys/menu")
@Tag(name = "菜单管理")
@AllArgsConstructor
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("list")
    @Operation(summary = "菜单列表")
    @Parameter(name = "type", description = "菜单类型 0：菜单 1：按钮  2：接口  null：全部")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public BaseResponse<List<SysMenuVO>> list(Integer type) {
        List<SysMenuVO> list = sysMenuService.getMenuList(type);

        return ResultUtils.success(list);
    }

    @GetMapping("authority")
    @Operation(summary = "用户权限标识")
    public BaseResponse<Set<String>> authority() {
        LoginUser user = SecurityUtil.getAuthentication();
        Set<String> set = sysMenuService.getUserAuthority(user);
        return ResultUtils.success(set);
    }

    @GetMapping("nav")
    @Operation(summary = "菜单导航")
    public BaseResponse<List<SysMenuVO>> nav() {
        LoginUser user = SecurityUtil.getAuthentication();
        List<SysMenuVO> list = sysMenuService.getUserMenuList(user, MenuTypeEnum.MENU.getValue());
        return ResultUtils.success(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:menu:info')")
    public BaseResponse<SysMenuVO> get(@PathVariable("id") Long id) {
        SysMenuEntity entity = sysMenuService.getById(id);
        SysMenuVO vo = SysMenuConvert.INSTANCE.convert(entity);

        // 获取上级菜单名称
        if (Constant.ROOT !=entity.getPid()) {
            SysMenuEntity parentEntity = sysMenuService.getById(entity.getPid());
            vo.setParentName(parentEntity.getName());
        }

        return ResultUtils.success(vo);
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public BaseResponse<?> save(@RequestBody  SysMenuVO vo) {
        sysMenuService.save(vo);

        return ResultUtils.success();
    }

    @PutMapping
    @Operation(summary = "修改")
   @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public BaseResponse<?> update(@RequestBody  SysMenuVO vo) {
        sysMenuService.update(vo);

        return ResultUtils.success();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
   @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public BaseResponse<?> delete(@PathVariable("id") Long id) {
        // 判断是否有子菜单或按钮
        Long count = sysMenuService.getSubMenuCount(id);
        if (count > 0) {
            return ResultUtils.error(500,"请先删除子菜单");
        }

        sysMenuService.delete(id);

        return ResultUtils.success();
    }

}
